//
//  KXNavigationControllerViewController.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/6.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class KXNavigationControllerViewController: UINavigationController,UINavigationControllerDelegate {
    
    var popDelegate : UIGestureRecognizerDelegate?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        
        let item = UIBarButtonItem.appearance()
        item.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Normal)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        popDelegate = self.interactivePopGestureRecognizer?.delegate
        self.interactivePopGestureRecognizer?.delegate = nil
        self.delegate = self
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count != 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.barButtonItemWithImage(UIImage(named: "navigationbar_back")!, highImage: UIImage(named: "navigationbar_back_highlighted")!, target: self, action: "popToPre", controlEvents: UIControlEvents.TouchUpInside)
            
            viewController.navigationItem.rightBarButtonItem = UIBarButtonItem.barButtonItemWithImage(UIImage(named: "navigationbar_more")!, highImage: UIImage(named: "navigationbar_more_highlighted")!, target: self, action: "popToRoot", controlEvents: UIControlEvents.TouchUpInside)
        }
        super.pushViewController(viewController, animated: animated)
    }

    func popToPre(){
        self.popViewControllerAnimated(true)
    }
    func popToRoot(){
        self.popToRootViewControllerAnimated(true)
    }
    
    //nav
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        
        if viewController == self.viewControllers[0] {
            self.interactivePopGestureRecognizer?.delegate = popDelegate
       
                    }else{
            self.interactivePopGestureRecognizer?.delegate = nil
        }
    }
    
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        let tarBarVc : UITabBarController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UITabBarController
        
        for tarBarButton in tarBarVc.tabBar.subviews {
            if !tarBarButton.isKindOfClass(TabBar) {
                tarBarButton.removeFromSuperview()
            }
        }
        

    }
}
